<!DOCTYPE html>
<html >
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    
    <title>ffmpeg视频解码丢帧问题 | xilixili.net</title>
    <meta name="renderer" content="webkit">
    <meta name="HandheldFriendly" content="True">
    <meta name="MobileOptimized" content="320">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <meta name="description" content="视频解码的时候，av_read_frame出来的包进行avcodec_decode_video2解码，有时候got_frame为0，即没有数据。一般来讲每一帧都是一帧图像，按道理肯定是有图像的，但却没有，依此下去最终在文件结尾处依然是少部分数据，难不成数据丢了？">

    <meta name="twitter:card" content="summary">
    <meta name="twitter:title" content="ffmpeg视频解码丢帧问题 | xilixili.net">
    <meta name="twitter:description" content="视频解码的时候，av_read_frame出来的包进行avcodec_decode_video2解码，有时候got_frame为0，即没有数据。一般来讲每一帧都是一帧图像，按道理肯定是有图像的，但却没有，依此下去最终在文件结尾处依然是少部分数据，难不成数据丢了？">

    <meta property="og:type" content="article">
    <meta property="og:title" content="ffmpeg视频解码丢帧问题 | xilixili.net">
    <meta property="og:description" content="视频解码的时候，av_read_frame出来的包进行avcodec_decode_video2解码，有时候got_frame为0，即没有数据。一般来讲每一帧都是一帧图像，按道理肯定是有图像的，但却没有，依此下去最终在文件结尾处依然是少部分数据，难不成数据丢了？">

    
    <meta name="author" content="rangerlee">
    
    <link rel="stylesheet" href="/css/vno.css">
    <link rel="stylesheet" href="/css/font-awesome.min.css">

    
    <link rel="icon" href="/images/avatar-small.png">
    

    <meta name="generator" content="hexo"/>
    
    <link rel="alternate" type="application/rss+xml" title="xilixili.net" href="/atom.xml">
    

    <link rel="canonical" href="xilixili.net/2017/12/19/ffmpeg-decode-video-frame-lost/"/>

                 
</head>

<body class="home-template no-js">
    <script src="//cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="/js/main.js"></script>
    <span class="mobile btn-mobile-menu">
        <i class="fa fa-list btn-mobile-menu__icon"></i>
        <i class="fa fa-angle-up btn-mobile-close__icon hidden"></i>
    </span>

    
<header class="panel-cover panel-cover--collapsed">
  <div class="panel-main">
    <div class="panel-main__inner panel-inverted">
    <div class="panel-main__content">

        <h1 class="panel-cover__title panel-title"><a href="/" title="访问首页 xilixili.net">xilixili.net</a></h1>
        
        <span class="panel-cover__subtitle panel-subtitle"><!-- 生活と技术の杂记 --></span>
        
        <hr class="panel-cover__divider">
        
        <p class="panel-cover__description">生活と技术の杂记</p>
        
	<hr class="panel-cover__divider panel-cover__divider--secondary">

        <div class="navigation-wrapper">
          <div>
          <nav class="cover-navigation cover-navigation--primary">
            <ul class="navigation">
              <li class="navigation__item"><a href="/#blog" title="访问博客" class="blog-button">博客</a></li>
            
            </ul>
          </nav>
          </div>
          <div>
          <nav class="cover-navigation navigation--social">
  <ul class="navigation">

  <!-- Weibo-->
  

  <!-- Github -->
  
  <li class="navigation__item">
    <a href="https://github.com/rangerlee" title="查看我的GitHub主页" target="_blank">
      <i class="social fa fa-github"></i>
      <span class="label">Github</span>
    </a>
  </li>


<!-- Stack Overflow -->
        

  <!-- Google Plus -->
  

<!-- Facebook -->

  
<!-- Twitter -->

  

  <li class="navigation__item">
    <a href="/atom.xml" title="RSS" target="_blank">
      <i class="social fa fa-rss"></i>
      <span class="label">RSS</span>
    </a>
  </li>


  <li class="navigation__item">
    <a href="mailto:http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=RzUmKSAiNSsiIgchKD8qJi4raSQoKg" title="邮件联系我" target="_blank">
      <i class="social fa fa-envelope"></i>
      <span class="label">Email</span>
    </a>
  </li>


  </ul>
</nav>

          </div>
        </div>

      </div>

    </div>

    <div class="panel-cover--overlay cover-alpha"></div>
  </div> 
</header>
<script type="text/javascript">
var side = document.getElementsByClassName("panel-cover")[0];
function checkWebp() {
	try{
		return(document.createElement('canvas').toDataURL('image/webp').indexOf('data:image/webp') == 0);
	}catch(err) {
		return false;
	}
};

if(checkWebp()) {
	side.style.backgroundImage = "url('/images/background-cover.jpg.webp')";
} else {
	side.style.backgroundImage = "url('/images/background-cover.jpg')";
}
</script>


    <div class="content-wrapper">
        <div class="content-wrapper__inner">
            <article class="post-container post-container--single">

  <header class="post-header">
    <div class="post-meta">
      <time datetime="2017-12-19T03:16:00.000Z" class="post-list__meta--date date">2017-12-19</time> &#8226; <span class="post-meta__tags tags">
  <a class="tag-link" href="/tags/技术/">技术</a>
 </span>
      <span class="page-pv">
       阅读 <span id="busuanzi_value_page_pv"><i class="fa fa-spinner fa-spin"></i></span>
      </span> 
   
    </div>
    <h1 class="post-title">ffmpeg视频解码丢帧问题</h1>
  </header>

  <section class="post">
    <p>视频解码的时候，<code>av_read_frame</code>出来的包进行<code>avcodec_decode_video2</code>解码，有时候got_frame为0，即没有数据。一般来讲每一帧都是一帧图像，按道理肯定是有图像的，但却没有，依此下去最终在文件结尾处依然是少部分数据，难不成数据丢了？</p>
<a id="more"></a>
<p>当然丢数据倒不至于，简单的说读取的<code>AVPacket</code>都有pts和dts两个属性，往复杂了说视频帧类型有I/P/B等种类，我们就以简单的方式说，PTS是图像的展示时间，DTS是图像的解码时间，问题就来了，由于视频帧类型，很多时候，PTS基本都是按顺序的，但是DTS却不是，也就是说这个包的解码时候需要在下个包解码之后解码，所以此次就不能解码，因此获取不到视频帧，数据被缓存了，如果之后不去主动去取，那就真的丢了。</p>
<figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">while</span>( av_read_frame(format_ctx_, &amp;packet) &gt;= <span class="number">0</span> ) &#123;</span><br><span class="line">    <span class="keyword">if</span>( packet.stream_index == video_stream_index_ ) &#123;</span><br><span class="line">        avcodec_decode_video2(codec_ctx_, frame, &amp;frameFinished, &amp;packet);</span><br><span class="line">        <span class="keyword">if</span>( frameFinished ) &#123;</span><br><span class="line">            <span class="comment">//...</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    av_free_packet(&amp;packet);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>以上的代码基本就是常见的使用场景，基本上文件的最后几帧已经丢失了，虽然很多时候不是太重要，可以忽略，毕竟几帧数据在很大帧率下所占的时间很小。如果需要的话，那如何找回来呢，简单的办法就是继续空包读取即可</p>
<figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">while</span>( av_read_frame(format_ctx_, &amp;packet) &gt;= <span class="number">0</span> ) &#123;</span><br><span class="line">    <span class="keyword">if</span>( packet.stream_index == video_stream_index_ ) &#123;</span><br><span class="line">        avcodec_decode_video2(codec_ctx_, frame.get(), &amp;frameFinished, &amp;packet);</span><br><span class="line">        <span class="keyword">if</span>( frameFinished ) &#123;	</span><br><span class="line">            <span class="comment">//...</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            total_skip++;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    av_free_packet(&amp;packet);</span><br><span class="line">&#125;</span><br><span class="line">    </span><br><span class="line"><span class="keyword">while</span>(total_skip--) &#123;</span><br><span class="line">    avcodec_decode_video2(codec_ctx_, frame.get(), &amp;frameFinished, &amp;packet);</span><br><span class="line">    <span class="keyword">if</span>( frameFinished ) &#123;</span><br><span class="line">        <span class="comment">//...</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>注意，空包解码的时候必须是packet的<code>data</code>为NULL,<code>size</code>为0。</p>

  </section>

</article>

<section class="read-more">
           
    
               
            <div class="read-more-item">
                <span class="read-more-item-dim">最近的文章</span>
                <h2 class="post-list__post-title post-title"><a href="/2017/12/25/release-without-library-reference-on-visualstudio/" title="VC的Release版本无法找到入口问题">VC的Release版本无法找到入口问题</a></h2>
                <p class="excerpt">
                
                在之前的版本上改了很多功能之后，一直用的Debug版本在本地调试，没有什么问题。今天给同事提供了老版本和新版本的Release的EXE程序，结果反馈新版本的程序运行不了。
缘由按照惯例，肯定是没安装redist_x86.exe环境，转念一想我们打包可是自带了msvcrt.dll等CRT库的，况且测试
                &hellip;
                </p>
                <div class="post-list__meta"><time datetime="2017-12-25T10:28:00.000Z" class="post-list__meta--date date">2017-12-25</time> &#8226; <span class="post-list__meta--tags tags">
  <a class="tag-link" href="/tags/技术/">技术</a>
</span><a class="btn-border-small" href="/2017/12/25/release-without-library-reference-on-visualstudio/">继续阅读</a></div>
                           
            </div>
        
        
               
            <div class="read-more-item">
                <span class="read-more-item-dim">更早的文章</span>
                <h2 class="post-list__post-title post-title"><a href="/2017/11/30/webrtc-live/" title="WebRTC直播技术">WebRTC直播技术</a></h2>
                <p class="excerpt">
                
                前文描述了正常的浏览器端采用WebRTC进行视频通话的方法，也介绍了当前流行的RTMP架构的直播模式，及相关技术等。那么面对WebRTC这项技术，我们是否能够将两者结合呢，即采用WebRTC来进行直播。整好项目中有这样的需求，因此做了一段时间的技术预研，简单分享下。
技术假设想使用WebRTC技术做
                &hellip;
                </p>
                <div class="post-list__meta"><time datetime="2017-11-30T09:52:00.000Z" class="post-list__meta--date date">2017-11-30</time> &#8226; <span class="post-list__meta--tags tags">
  <a class="tag-link" href="/tags/技术/">技术</a>
</span><a class="btn-border-small" href="/2017/11/30/webrtc-live/">继续阅读</a></div>
                       
            </div>
        
     
   
   
  
</section>



	<script>
	(function(){
		var bp = document.createElement('script');
		var curProtocol = window.location.protocol.split(':')[0];
		if (curProtocol === 'https') {
			bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
		}
		else {
			bp.src = 'http://push.zhanzhang.baidu.com/push.js';
		}
		var s = document.getElementsByTagName("script")[0];
		s.parentNode.insertBefore(bp, s);
	})();
	</script>



<script>
(function(){
var src = (document.location.protocol == "http:") ? "http://js.passport.qihucdn.com/11.0.1.js?8612a7fa26d7ba4562be733ae33649b1":"https://jspassport.ssl.qhimg.com/11.0.1.js?8612a7fa26d7ba4562be733ae33649b1";
document.write('<script src="' + src + '" id="sozz"><\/script>');
})();
</script>



            <footer class="footer">
    <span class="footer__copyright">
        &copy; 2023 rangerlee - 本站点采用 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/">知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议</a>
       
    </span>
    <span class="footer__copyright">
             - 本站基于开源 <a href="http://hexo.io">Hexo</a> 搭建，采用 <a href="https://github.com/rangerlee/hexo-theme-new-vno">hexo-theme-new-vno</a> 主题，修改自 <a href="https://github.com/monniya/hexo-theme-new-vno ">new-vno</a> 主题  <a href="http://www.beian.miit.gov.cn">皖ICP备16009457号</a>
         </span>
       
    
    
</footer>


        </div>
    </div>

     
    
    <script>
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "//hm.baidu.com/hm.js?bab4f71524319c10819bcae280021624";
            var s = document.getElementsByTagName("script")[0]; 
            s.parentNode.insertBefore(hm, s);
        })();
    </script>



    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    
    
</body>
</html>
